package iamutkarshtiwari.github.io.ananas.editimage.view;


import iamutkarshtiwari.github.io.ananas.editimage.utils.TouchEventCompact;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.TouchEvent;

import java.math.BigDecimal;
import java.util.LinkedHashMap;

public class StickerView extends Component implements Component.DrawTask, Component.TouchEventListener {
    private static final int STATUS_IDLE = 0;
    private static final int STATUS_MOVE = 1;
    private static final int STATUS_DELETE = 2;
    private static final int STATUS_ROTATE = 3;

    private int imageCount;
    //    private Context mContext;
    private int currentStatus;
    private StickerItem currentItem;
    private float oldx, oldy;

    private Paint rectPaint = new Paint();
    //    private Paint boxPaint = new Paint();
    private TouchEventCompact compact;

    private LinkedHashMap<Integer, StickerItem> bank = new LinkedHashMap<Integer, StickerItem>();

    public StickerView(Context context) {
        super(context);
        init(context);
    }

    public StickerView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context);
    }

    public StickerView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化
     *
     * @param context 上下文
     */
    private void init(Context context) {
//        this.mContext = context;
        currentStatus = STATUS_IDLE;

        rectPaint.setColor(Color.RED);
        rectPaint.setAlpha(100);

        compact = new TouchEventCompact();
        addDrawTask(this);
        setTouchEventListener(this);
    }

    /**
     * 增加需要添加的图
     *
     * @param addBit 添加的图
     */
    public void addBitImage(final PixelMap addBit) {
        StickerItem item = new StickerItem(this.getContext());
        item.init(addBit, this);
        if (currentItem != null) {
            currentItem.isDrawHelpTool = false;
        }
        bank.put(++imageCount, item);
        this.invalidate();
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        onDraw(this, mCanvasForTaskOverContent);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        for (Integer id : bank.keySet()) {
            StickerItem item = bank.get(id);

            item.draw(canvas);
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        boolean ret = false;
        compact.correctY(touchEvent);
        int action = touchEvent.getAction();
        float x = touchEvent.getPointerPosition(touchEvent.getIndex()).getX();
        float y = touchEvent.getPointerPosition(touchEvent.getIndex()).getY();
        switch (action) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                int deleteId = -1;
                for (Integer id : bank.keySet()) {
                    StickerItem item = bank.get(id);
                    if (item.detectDeleteRect2.isInclude(x, y)) {
                        // ret = true;
                        deleteId = id;
                        currentStatus = STATUS_DELETE;
                    } else if (item.detectRotateRect.isInclude(x, y)) {
                        ret = true;
                        if (currentItem != null) {
                            currentItem.isDrawHelpTool = false;
                        }
                        currentItem = item;
                        currentItem.isDrawHelpTool = true;
                        currentStatus = STATUS_ROTATE;
                        oldx = x;
                        oldy = y;
                    } else if (item.dstRect.isInclude(x, y)) {
                        ret = true;
                        if (currentItem != null) {
                            currentItem.isDrawHelpTool = false;
                        }
                        currentItem = item;
                        currentItem.isDrawHelpTool = true;
                        currentStatus = STATUS_MOVE;
                        oldx = x;
                        oldy = y;
                    }
                }

                if (!ret && currentItem != null && currentStatus == STATUS_IDLE) {
                    currentItem.isDrawHelpTool = false;
                    currentItem = null;
                    invalidate();
                }

                if (deleteId > 0 && currentStatus == STATUS_DELETE) {
                    bank.remove(deleteId);
                    currentStatus = STATUS_IDLE;
                    invalidate();
                }

                break;
            case TouchEvent.POINT_MOVE:
                ret = true;
                if (currentStatus == STATUS_MOVE) {
                    float dx = BigDecimal.valueOf(x).subtract(BigDecimal.valueOf(oldx)).floatValue();
                    float dy = BigDecimal.valueOf(y).subtract(BigDecimal.valueOf(oldy)).floatValue();
                    if (currentItem != null) {
                        currentItem.updatePos(dx, dy);
                        invalidate();
                    }
                    oldx = x;
                    oldy = y;
                } else if (currentStatus == STATUS_ROTATE) {
                    float dx = BigDecimal.valueOf(x).subtract(BigDecimal.valueOf(oldx)).floatValue();
                    float dy = BigDecimal.valueOf(y).subtract(BigDecimal.valueOf(oldy)).floatValue();
                    if (currentItem != null) {
                        currentItem.updateRotateAndScale(oldx, oldy, dx, dy);
                        invalidate();
                    }
                    oldx = x;
                    oldy = y;
                }
                break;
            case TouchEvent.CANCEL:
            case TouchEvent.PRIMARY_POINT_UP:
                ret = false;
                currentStatus = STATUS_IDLE;
                break;
        }
        return ret;
    }

    /**
     * 获取所有需要添加的item集合
     *
     * @return 返回所有需要添加的item集合
     */
    public LinkedHashMap<Integer, StickerItem> getBank() {
        return bank;
    }

    /**
     * 清空添加的图，并刷新
     */
    public void clear() {
        bank.clear();
        this.invalidate();
    }
}
